import { request, type ApiResponse } from './request'

// 请假申请请求参数
export interface LeaveRequest {
  studentId: number | string // 支持大整数，可能是字符串形式以避免精度丢失
  leaveType: number // 1:病假 2:事假
  urgencyLevel: number // 1:一般 2:严重
  emergencyContact: string // 紧急联系人
  emergencyPhone: string // 紧急联系电话
  offCampusAddress?: string // 校外详细去向 (当destinationType为2时必填)
  reason: string // 请假原因
  destinationType: number // 1:校内 2:校外
  proofMaterialUrl?: string // 证明材料URL
  startTime?: string // 开始时间 (ISO 8601 格式)
  endTime?: string // 结束时间 (ISO 8601 格式)
}

// 请假申请响应
export interface LeaveResponse {
  id?: number
  [key: string]: any
}

// 请假申请列表项
export interface LeaveListItem {
  id?: number
  studentId?: number | string
  leaveType?: number
  urgencyLevel?: number
  emergencyContact?: string
  emergencyPhone?: string
  destinationType?: number
  offCampusAddress?: string
  reason?: string
  proofMaterialUrl?: string
  startTime?: string
  endTime?: string
  status?: number | string
  approvalStatus?: number // 审批状态：0-待审批，1-待班主任审批，2-待辅导员审批（班主任已通过），3-院级审批中，4-审批通过，5-审批驳回
  [key: string]: any
}

/**
 * 提交请假申请
 * @param data 请假申请数据
 * @returns Promise<ApiResponse<LeaveResponse>>
 */
export const submitLeave = async (data: LeaveRequest): Promise<ApiResponse<LeaveResponse>> => {
  return await request<LeaveResponse>('leave', {
    method: 'POST',
    body: data
  })
}

/**
 * 获取学生请假申请列表
 * @param studentId 学生ID
 * @returns Promise<ApiResponse<LeaveListItem[]>>
 */
export const getLeaveListByStudentId = async (studentId: number | string): Promise<ApiResponse<LeaveListItem[]>> => {
  return await request<LeaveListItem[]>(`leave/student/${studentId}`, {
    method: 'GET'
  })
}

// 待审批请假申请列表项
export interface PendingLeaveItem {
  id?: number
  studentId?: number | string
  studentName?: string
  studentNum?: string
  leaveType?: number
  urgencyLevel?: number
  emergencyContact?: string
  emergencyPhone?: string
  destinationType?: number
  offCampusAddress?: string
  reason?: string
  proofMaterialUrl?: string
  startTime?: string
  endTime?: string
  status?: number | string
  approvalStatus?: number // 审批状态：0-待审批，1-待班主任审批，2-待辅导员审批，3-院级审批中，4-审批通过，5-审批驳回
  [key: string]: any
}

/**
 * 获取待审批请假申请列表
 * @param approverRole 审批角色：1-老师，2-辅导员
 * @returns Promise<ApiResponse<PendingLeaveItem[]>>
 */
export const getPendingLeaveList = async (approverRole: number): Promise<ApiResponse<PendingLeaveItem[]>> => {
  return await request<PendingLeaveItem[]>(`leave/pending/${approverRole}`, {
    method: 'GET'
  })
}

/**
 * 根据教师ID获取请假申请列表
 * @param userId 教师ID
 * @returns Promise<ApiResponse<PendingLeaveItem[]>>
 */
export const getLeaveListByTeacherId = async (userId: number | string): Promise<ApiResponse<PendingLeaveItem[]>> => {
  return await request<PendingLeaveItem[]>(`leave/teacher/${userId}`, {
    method: 'GET'
  })
}

// 审批请假申请请求参数
export interface ApproveLeaveRequest {
  leaveId: number
  approverRole: number
  comment: string
  approved: boolean
}

/**
 * 审批请假申请
 * @param data 审批数据
 * @returns Promise<ApiResponse<any>>
 */
export const approveLeave = async (data: ApproveLeaveRequest): Promise<ApiResponse<any>> => {
  return await request<any>('leave/approve', {
    method: 'POST',
    body: data
  })
}

// 撤销请假申请请求参数
export interface RevokeLeaveRequest {
  leaveId: number | string
  comment: string
}

/**
 * 撤销请假申请
 * @param data 撤销数据
 * @returns Promise<ApiResponse<any>>
 */
export const revokeLeave = async (data: RevokeLeaveRequest): Promise<ApiResponse<any>> => {
  return await request<any>('leave/revocate', {
    method: 'POST',
    body: data
  })
}

/**
 * 销假
 * @param leaveId 请假ID
 * @returns Promise<ApiResponse<any>>
 */
export const returnLeave = async (leaveId: number | string): Promise<ApiResponse<any>> => {
  return await request<any>('leave/return', {
    method: 'GET',
    params: { leaveId }
  })
}

